<template>
  <a-card>
    <div>
      <!-- 搜索 -->
      <a-form v-bind="layout">
        <a-row>
          <!-- 单据号 -->
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="单据号">
              <a-select
                v-model="searchForm.document"
                @change="getOrderId"
                showSearch
                style="width: 100%"
              >
                <a-select-option
                  v-for="item in OrderList"
                  :key="item.id"
                  :value="item.document"
                >
                  <!-- {{ item.document }} -->
                  {{ item.document }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <!-- 仓库名称 -->
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="仓库名称">
              <a-select
                v-model="searchForm.depotName"
                @change="changeDepotId"
                showSearch
                style="width: 100%"
              >
                <a-select-option
                  v-for="item in storageList"
                  :key="item.depotId"
                  :value="item.depotNo + '|' + item.depotName"
                  >{{ item.depotNo }} {{ item.depotName }}</a-select-option
                >
              </a-select>
            </a-form-model-item>
          </a-col>
          <!-- 存货名称 -->
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="成套件">
              <a-select
                v-model="searchForm.inventoryName"
                @change="getInventoryId"
                showSearch
                style="width: 100%"
              >
                <a-select-option
                  v-for="item in partsInventoryInformationList"
                  :key="item.inventoryId"
                  :value="item.inventoryNo + '|' + item.inventoryName"
                >
                  {{ item.inventoryNo }} {{ item.inventoryName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <!-- 制单日期 -->
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="制单日期" ref="sort" prop="sort">
              <a-range-picker
                style="width: 100%"
                v-model="searchForm.sort"
                @change="getRangeTime"
              />

              <!-- <a-date-picker
                style="width: 100%"
                v-model="searchForm.formDate"
                @change="getRangeTime"
              /> -->
            </a-form-model-item>
          </a-col>

          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="审核状态" ref="sort" prop="sort">
              <a-select v-model="searchForm.auditMark" @change="getCheckTag">
                <a-select-option value="1">已审核</a-select-option>
                <a-select-option value="2">未审核</a-select-option>
              </a-select>

              <!-- <a-radio-group
                v-model="searchForm.auditMark"
                @change="getCheckTag"
              >
                <a-radio value="1"> 已审核 </a-radio>
                <a-radio value="2"> 未审核 </a-radio>
              </a-radio-group> -->
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="制单人">
              <a-select
                v-model="searchForm.prepared"
                style="width: 100%"
                showSearch
              >
                <a-select-option
                  v-for="(item, index) in PreparedyList"
                  :key="index"
                  :value="item.id"
                >
                  {{ item.id_dictText }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="同步状态">
              <a-select v-model="searchForm.isSynchronized" style="width: 100%">
                <a-select-option
                  v-for="item in isSynchronizedList"
                  :value="item"
                  :key="item"
                >
                  {{ item }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col
            :xl="24"
            :lg="24"
            :md="24"
            :sm="24"
            :xs="24"
            :style="{ textAlign: 'right' }"
          >
            <a-button type="primary" @click="Search()">查询</a-button>
            <a-button style="margin-left: 8px" @click="Reset()">重置</a-button>
          </a-col>
        </a-row>
      </a-form>

      <a-table
        :columns="columns"
        :data-source="dataSource"
        :pagination="pagination"
        @change="handleTableChange"
        style="margin-top: 20px"
        :scroll="{ x: 1500 }"
        size="small"
      >
        <span slot="document" slot-scope="text, record">
          <a @click="RouterJump(record)"> {{ text }} </a>
        </span>

        <template slot="options" slot-scope="text, record">
          <a-popconfirm
            v-if="record.auditMark_dictText == '未审核'"
            ok-text="确定"
            cancel-text="取消"
            @confirm="Delete(record.id)"
          >
            <template slot="title">
              <p>确定删除？</p>
            </template>
            <a type="danger"> 删除 </a>
          </a-popconfirm>

          <!-- <a
          v-if="record.auditMark_dictText == '已审核'"
          style="color: #666666"
          :disabled="showchuguo"
        >
          <a-tooltip>
            <template slot="title"> 不允许删除 </template>
            删除
          </a-tooltip>
        </a> -->

          <a
            v-if="record.auditMark_dictText == '已审核'"
            style="color: #666666"
          >
            <a-tooltip>
              <template slot="title"> 不允许删除 </template>
              删除
            </a-tooltip>
          </a>
        </template>
      </a-table>
    </div>
  </a-card>
</template>
<script>
import { CommonAjax } from "@/services/modules/common";
const comHttp = new CommonAjax(); // 公用接口

import { BeiHuoTongZhiHttp } from "@/services/modules/ChuKuGuanLi/XiaoShouChuKu/BeiHuoTongZhi";
const bhtzjHttp = new BeiHuoTongZhiHttp(); // 备货通知

const columns = [
  {
    title: "序号",
    dataIndex: "key",
    key: "key",
    align: "center",
    width: "50px",
  },
  {
    title: "单据日期",
    dataIndex: "documentDate",
    key: "documentDate",
    align: "center",
    width: "100px",
  },
  {
    title: "审核标志",
    dataIndex: "auditMark_dictText",
    key: "auditMark_dictText",
    align: "center",
    width: "80px",
  },
  {
    title: "用友同步",
    dataIndex: "isSynchronized",
    key: "isSynchronized",
    align: "center",
    width: "80px",
  },
  {
    title: "反审核",
    dataIndex: "isAntiAudit",
    key: "isAntiAudit",
    align: "center",
    width: "80px",
  },
  {
    title: "单据号",
    dataIndex: "document",
    key: "document",
    align: "center",
    width: "120px",
    scopedSlots: { customRender: "document" },
  },
  {
    title: "组装类型",
    dataIndex: "type_dictText",
    key: "type_dictText",
    align: "center",
    width: "80px",
  },
  // {
  //   title: "仓库编码",
  //   dataIndex: "depotNo",
  //   key: "depotNo",
  //   align: "center",
  // },
  {
    title: "仓库名称",
    dataIndex: "depotId_dictText",
    key: "depotId_dictText",
    align: "center",
    width: "100px",
  },
  // {
  //   title: "库位",
  //   key: "depotLocation_dictText",
  //   dataIndex: "depotLocation_dictText",
  //   align: "center",
  // },

  {
    title: "仓位",
    key: "position_dictText",
    dataIndex: "position_dictText",
    align: "center",
    width: "80px",
    scopedSlots: { customRender: "position_dictText" },
  },

  {
    title: "存货编码",
    dataIndex: "inventoryNo",
    key: "inventoryNo",
    align: "center",
    width: "100px",
  },
  {
    title: "存货名称",
    dataIndex: "inventoryId_dictText",
    key: "inventoryId_dictText",
    align: "center",
    width: "100px",
  },
  {
    title: "规格型号",
    dataIndex: "specificationsModel",
    key: "specificationsModel",
    align: "center",
    width: "80px",
  },
  {
    title: "单位",
    dataIndex: "unit",
    key: "unit",
    align: "center",
    width: "50px",
  },
  {
    title: "数量",
    dataIndex: "number",
    key: "number",
    align: "center",
    width: "50px",
  },
  // {
  //   title: "单价",
  //   dataIndex: "univalent",
  //   key: "univalent",
  //   align: "center",
  // },
  // {
  //   title: "金额",
  //   dataIndex: "money",
  //   key: "money",
  //   align: "center",
  // },
  {
    title: "收发类别",
    key: "sendAndReceiveCategory_dictText",
    dataIndex: "sendAndReceiveCategory_dictText",
    align: "center",
    width: "80px",
  },
  {
    title: "批次号",
    dataIndex: "batchNo",
    key: "batchNo",
    align: "center",
    width: "100px",
  },
  {
    title: "转库日期",
    key: "transferDate",
    dataIndex: "transferDate",
    width: "100px",
    align: "center",
  },
  {
    title: "制单人",
    dataIndex: "preparedBy_dictText",
    key: "preparedBy_dictText",
    align: "center",
    width: "80px",
  },
  {
    title: "记账标志",
    dataIndex: "bookkeepingMark_dictText",
    key: "bookkeepingMark_dictText",
    width: "80px",
    align: "center",
  },
  {
    title: "审核人",
    dataIndex: "reviewer_dictText",
    key: "reviewer_dictText",
    align: "center",
    width: "80px",
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    align: "center",
    width: "100px",
  },
  // {
  //   title: "操作",
  //   dataIndex: "options",
  //   key: "options",
  //   align: "center",
  //   scopedSlots: { customRender: "options" },
  // },
];

export default {
  data() {
    return {
      isSynchronizedList: ["已同步", "未同步"],
      searchForm: {
        isSynchronized: "",
        document: "",
        prepared: "",
        depotName: "",
        depotId: "",

        inventoryId: "",
        inventoryName: "",
        formDate: "",
        sort: null,
        startTime: "",
        endTime: "",
        auditMark: "2",
      },

      // 格式
      layout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 15 },
      },

      //
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10, //每页中显示10条数据

        pageSizeOptions: ["5", "10", "20", "30"], //每页中显示的数据
        showTotal: (total) => `共有${total}条数据`, //分页中显示总的数据
        showSizeChanger: true, // 显示页面条数改变
        showQuickJumper: true, // 显示快速跳转
      },
      queryParam: {
        //查询参数
        page: 1, //第几页
        size: 10, //每页中显示数据的条数
        hosName: "",
        hosCode: "",
        province: "",
        city: "",
      },
      dataSource: [],
      columns,

      ALLOrderList: [], // 所有的单据列表
      storageList: [], // 倉庫列表
      OrderList: [], // 单据列表
      PreparedyList: [], //制单人列表
      partsInventoryInformationList: [], // 成套件
    };
  },
  mounted() {
    this.getPartsInventoryInformationList();
    this.getAssemblyOrderAndGoodsList();
    this.getStorageList();
    this.getAssemblyListAndId();
    this.getPreparedyList(); //获取制单人列表
  },

  watch: {
    $route: function (to, form) {
      if (to.path == "/AssembleList" && form.path == "/AssembleDetail") {
        console.log("--------- 更新视图 列表  ----------");
        this.Reset();
        this.getAssemblyOrderAndGoodsList();
      }
    },
  },

  // 路由跳转
  methods: {
    // 获取制单人列表
    getPreparedyList() {
      comHttp.getPrepareBy().then((res) => {
        var pattern = new RegExp("[\u4E00-\u9FA5]+");

        console.log("获取制单人列表:::::", res);
        let data = res.result.list.filter((item) => {
          if (pattern.test(item.id)) {
            console.log(item.id);
          } else if (item.id == null) {
            console.log(item.id);
          } else {
            return item;
          }
        });
        console.log(data);
        this.PreparedyList = data;
      });
    },
    //
    getCheckTag() {
      let queryPath = this.getQueryPath();
      this.getAssemblyOrderAndGoodsList(queryPath);
    },
    // 路由跳转
    RouterJump(record) {
      // 审核人
      if (record.reviewer == this.$store.state.user.info.id) {
        console.log("这是审核人,能审核,能提交");
        this.$router.push({
          path: "AssembleDetail",
          query: {
            Id: record.id,
          },
        });
      }
      // 制单人能修改
      else if (record.preparedBy == this.$store.state.user.info.id) {
        console.log("这是制单人,能修改");
        // 如果未审核，则能修改，审核过了则不能修改
        if (record.auditMark == 2) {
          this.$router.push({
            path: "Assemble",
            query: {
              Id: record.id,
            },
          });
        } else {
          // 审核过了,不能修改
          this.$router.push({
            path: "AssembleDetail",
            query: {
              Id: record.id,
            },
          });
        }
      }
      // 其余人员
      else {
        console.log("这是其余人员,仅能查看，不能审核和提交");
        this.$router.push({
          path: "AssembleDetail",
          query: {
            Id: record.id,
          },
        });
      }
    },

    // 获取仓库编码
    changeDepotId(value, option) {
      this.searchForm.depotId = option.key;
      console.log("仓库id::::", this.searchForm.depotId);
    },
    // 获取存货id
    getInventoryId(value, option) {
      this.searchForm.inventoryId = option.key;
      console.log("存货id::::", this.searchForm.inventoryId);
    },
    // 获取时间段
    getRangeTime(moment, date) {
      console.log(moment);
      console.log(date);
      // this.searchForm.formDate = date;、

      this.searchForm.startTime = date[0];
      this.searchForm.endTime = date[1];
    },

    // 获取仓库列表
    getStorageList() {
      this.storageList = [];
      // 获取仓库名称
      comHttp.getAllNumberName_new_forU8c().then((res) => {
        console.log("获取所有仓库", res);
        for (let i = 0; i < res.result.length; i++) {
          let data = {
            depotName: res.result[i].storname,
            depotNo: res.result[i].storcode,
            depotId: res.result[i].pkStordoc,
          };
          this.storageList.push(data);
        }
      });
    },

    // ----------------  查询 ----------------
    // 获取成套件列表
    getPartsInventoryInformationList() {
      comHttp.getAllPackageList().then((res) => {
        console.log("所有的成套件", res);
        for (let i = 0; i < res.result.list.length; i++) {
          let data = {
            inventoryId: res.result.list[i].partsInventoryInformationId, // 成套件Id
            inventoryName:
              res.result.list[i].partsInventoryInformationId_dictText, // 成套件名称
          };
          this.partsInventoryInformationList.push(data);
        }
      });
    },

    // 获取组装单号和物品列表
    getAssemblyOrderAndGoodsList(
      queryPath = "?pageNo=1&pageSize=10&auditMark=2"
    ) {
      bhtzjHttp.getAssemblyOrderAndGoodsList(queryPath).then((res) => {
        this.dataSource = [];
        this.ALLOrderList = [];
        console.log("获取组装列表", res);
        const pagination = { ...this.pagination };
        pagination.total = res.result.total;
        pagination.pageSize = res.result.size;
        this.pagination = pagination;

        for (let i = 0; i < res.result.records.length; i++) {
          let data = {
            id: res.result.records[i].id,
            key: (pagination.current - 1) * res.result.size + i + 1,
            documentDate: res.result.records[i].documentTime, // 单据日期
            auditMark: res.result.records[i].auditMark, // 审核标志
            auditMark_dictText: res.result.records[i].auditMark_dictText, // 审核标志
            isSynchronized: res.result.records[i].isSynchronized, // 审核标志
            isAntiAudit: res.result.records[i].isAntiAudit, // 审核标志
            bookkeepingMark: res.result.records[i].bookkeepingMark, // 记账标志
            bookkeepingMark_dictText:
              res.result.records[i].bookkeepingMark_dictText, // 记账标志
            depotId_dictText: res.result.records[i].depotId_dictText, // 仓库名称
            type_dictText: res.result.records[i].type_dictText, // 组装类型
            inventoryNo: res.result.records[i].invcode, // 存货编码

            position_dictText: res.result.records[i].position_dictText, // 仓位
            depotLocation_dictText:
              res.result.records[i].depotLocation_dictText, // 库位

            inventoryId_dictText: res.result.records[i].inventoryId_dictText, // 存货名称
            sendAndReceiveCategory_dictText:
              res.result.records[i].sendAndReceiveCategory_dictText, // 收发类别

            specificationsModel: res.result.records[i].invspec, // 规格型号
            unit: res.result.records[i].measname, // 单位
            number: res.result.records[i].number, // 数量
            money: res.result.records[i].money, // 单价
            // id: res.result.records[i].id,// 金额

            // 收发类别
            batchNo: res.result.records[i].batchNo, // 批次号
            transferDate: res.result.records[i].transferDate, // 转库日期

            preparedBy_dictText: res.result.records[i].preparedBy_dictText, // 制单人
            preparedBy: res.result.records[i].preparedBy,
            reviewer: res.result.records[i].reviewer,

            reviewer_dictText: res.result.records[i].reviewer_dictText, // 制单人
            // 审核日期
            remark: res.result.records[i].remark, // 备注
          };

          if (this.ALLOrderList.indexOf(res.result.records[i].document) > -1) {
            console.log("没有重复");
            this.dataSource.push(data);
          } else {
            console.log("已经重复");
            this.ALLOrderList.push(res.result.records[i].document);
            data[`document`] = res.result.records[i].document;
            this.dataSource.push(data);
          }
        }
      });
    },

    // 获取查询条件
    getQueryPath() {
      console.log(this.searchForm);
      let queryPath =
        "?pageNo=" +
        this.queryParam.page +
        "&pageSize=" +
        this.queryParam.size +
        "&document=" +
        this.searchForm.document + // 单据号
        "&auditMark=" +
        this.searchForm.auditMark + // 是否审核
        "&depotId=" +
        this.searchForm.depotId + // 仓库名称
        "&kit=" +
        this.searchForm.inventoryId +
        // "&docmentTime=" +
        // this.searchForm.formDate;

        // + // 存货名称
        "&beginDate=" +
        this.searchForm.startTime + //
        "&endDate=" +
        this.searchForm.endTime +
        "&preparedBy=" +
        this.searchForm.prepared+
        "&isSynchronized=" +
        this.searchForm.isSynchronized; //
      console.log("返回的查询路径:::", queryPath);
      return queryPath; // 返回的查询条件
    },

    // 重置当前页码及页码参数
    resetPagination() {
      this.pagination = {
        total: 0,
        current: 1,
        pageSize: 10, //每页中显示10条数据
        showSizeChanger: true,
        pageSizeOptions: ["5", "10", "15"], //每页中显示的数据
        showTotal: (total) => `共有${total}条数据`, //分页中显示总的数据
      };
      //查询参数
      this.queryParam = {
        page: 1, //第几页
        size: 10, //每页中显示数据的条数
        hosName: "",
        hosCode: "",
        province: "",
        city: "",
      };
    },

    // 分页函数
    handleTableChange(pagination) {
      this.pagination.current = pagination.current;
      this.pagination.pageSize = pagination.pageSize;
      this.queryParam.page = pagination.current;
      this.queryParam.size = pagination.pageSize;
      let queryPath = this.getQueryPath();
      this.getAssemblyOrderAndGoodsList(queryPath);
    },

    // 查询
    Search() {
      let queryPath = this.getQueryPath();
      this.getAssemblyOrderAndGoodsList(queryPath);
      console.log("查询条件为:::", queryPath);
    },

    Reset() {
      this.searchForm = {
        document: "",
        depotName: "",
        depotId: "",
        inventoryId: "",
        inventoryName: "",
        isSynchronized:"",
        sort: null,
        formDate: "",
        startTime: "",
        endTime: "",
        auditMark: "2",
        prepared: "",
      };
      this.resetPagination();
      this.getAssemblyOrderAndGoodsList();
    },

    Delete(id) {
      bhtzjHttp.deleteAssemblyOrderAndGoods("?id=" + id).then((res) => {
        if (res && res.code === 200) {
          this.$message.success("删除成功");
          this.getAssemblyOrderAndGoodsList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },

    // 获取的单据号列表
    getAssemblyListAndId() {
      bhtzjHttp.getAssemblyListAndId().then((res) => {
        for (let i = 0; i < res.result.length; i++) {
          console.log("res.result", res.result);
          let data = {
            id: res.result[i].id,
            document: res.result[i].document,
          };
          this.OrderList.push(data);
        }
      });
    },
    // 获取单据Id
    getOrderId(value, options) {
      this.searchForm.documentId = options.key;
    },
  },
};
</script>
